//
//  GrowthParadiseViewController.swift
//  BLH
//
//  Created by zhangshumeng on 2021/8/11.
//

import LTScrollView

let glt_iphoneX = (UIScreen.main.bounds.height >= 812.0)

class GrowthParadiseViewController: BaseViewController {

    private lazy var navLeftView: HomeNavView = {
        let view = HomeNavView()
        return view
    }()
    
    private lazy var viewControllers: [UIViewController] = {
        let vc1 = GPColumnViewController()
        let vc2 = GPVideoViewController()
        let vc3 = GPBookViewController()
        let vc4 = GPAudioViewController()
        return [vc1, vc2, vc3, vc4]
    }()

    private lazy var titles: [String] = {
        ["专栏", "视频", "绘本", "音频"]
    }()

    private lazy var layout: LTLayout = {
        let layout = LTLayout()
        layout.isAverage = true
        layout.isNeedScale = false
        layout.sliderWidth = 28
        layout.bottomLineHeight = 4
        layout.bottomLineCornerRadius = 2
        layout.bottomLineColor = UIColor.theme
        layout.pageBottomLineColor = UIColor.line
        layout.pageBottomLineHeight = 0.5
        layout.titleSelectColor = UIColor.theme
        layout.titleViewBgColor = UIColor.white
        layout.titleColor = UIColor.titleColor
        layout.titleFont = 14.font
        return layout
    }()

    private func managerReact() -> CGRect {
        let statusBarH = UIApplication.shared.statusBarFrame.size.height
        let Y: CGFloat = statusBarH + 44
        let H: CGFloat = glt_iphoneX ? (view.bounds.height - Y - 34) : view.bounds.height - Y
        return CGRect(x: 0, y: Y, width: view.bounds.width, height: H)
    }

    private lazy var headerView: GrowthParadiseHeaderView = {
        let view = GrowthParadiseHeaderView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 200))
        view.backgroundColor = .red
        return view
    }()

    private lazy var simpleManager: LTSimpleManager = {
        let simpleManager = LTSimpleManager(frame: managerReact(), viewControllers: viewControllers, titles: titles, currentViewController: self, layout: layout)
        simpleManager.delegate = self
        simpleManager.configHeaderView { [weak self] in
            guard let `self` = self else { return nil }
            return self.headerView
        }
        return simpleManager
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNav()
        addSubviews()
    }
    
    private func setupNav() {
        navigation.bar.setBackgroundImage("nav_bg_all_88".image, for: .default)
        navigation.item.leftBarButtonItem = UIBarButtonItem(customView: navLeftView)
    }

    private func addSubviews() {
        automaticallyAdjustsScrollViewInsets = false

        view.addSubview(simpleManager)
        self.simpleManager.glt_headerHeight = 200
        
//        headerView.heightChange = { [weak self] h in
//            guard let `self` = self else {
//                return
//            }
//            self.simpleManager.glt_headerHeight = h
//        }
    }

    private func showAudioIcon() {
        if !AudioPlayerViewController.share.playerItemModel.id.isEmpty {
            navigation.item.rightBarButtonItem = UIBarButtonItem(image: "icon_nav_audio".image, style: .plain, target: self, action: #selector(audioPlayerAction))
        } else {
            navigation.item.rightBarButtonItem = nil
        }
    }
    
    @objc private func audioPlayerAction() {
        push(AudioPlayerViewController.share)
    }
}

extension GrowthParadiseViewController {
    
}

extension GrowthParadiseViewController: LTSimpleScrollViewDelegate {
    // MARK: 滚动代理方法

    func glt_scrollViewDidScroll(_: UIScrollView) {
    }

    // MARK: 控制器刷新事件代理方法

    func glt_refreshScrollView(_ scrollView: UIScrollView, _ index: Int) {
    }
}
